package com.hnkywl.sts.dao.djgl;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.sql.Date;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-15
 * Time: 下午9:13
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class DjcxDao extends SimpleHibernateTemplate {

    public Map<String, Object> djcxList(String dhhm, Date djcxStart, Date djcxEnd, Integer pageNo, Integer pageSize) {
        Calendar calendar = Calendar.getInstance();
        //得到月末
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        java.util.Date endTimeUtil = calendar.getTime();
        //得到月初
        calendar.set(2011, 12, 1, 0, 0, 0);
        java.util.Date startTimeUtil = calendar.getTime();
        java.sql.Date endTime = new java.sql.Date(endTimeUtil.getTime());
        java.sql.Date startTime = new java.sql.Date(startTimeUtil.getTime());
        if (dhhm == null)
            dhhm = "-1";
        if (djcxStart == null)
            djcxStart = startTime;
        if (djcxEnd == null)
            djcxEnd = endTime;
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        Integer total = 0;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call VOUCHER_DEL(?,?,?,?,?,?,?)}");
            call.setString(1, dhhm);
            call.setDate(2, djcxStart);
            call.setDate(3, djcxEnd);
            call.setInt(4, (pageNo - 1) * pageSize);
            call.setInt(5, pageNo * pageSize);
            call.registerOutParameter(6, Types.NUMERIC);
            call.registerOutParameter(7, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            total = call.getInt(6);
            rs = ((OracleCallableStatement) call).getCursor(7);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                rows.add(r);
            }
            result.put("total", total);
            result.put("rows", rows);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }

}
